//
//  ChatRecodeViewController.m
//  AAIM
//
//  Created by 远的小布丁 on 2020/1/11.
//  Copyright © 2020 AAIM. All rights reserved.
//

#import "ChatRecodeViewController.h"
#import "ChatRecodeTableViewCell.h"
#import "ChatViewController.h"

@interface ChatRecodeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * chatRecodTableView;
@property (nonatomic,copy)NSString * titleStr;
@end

@implementation ChatRecodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.chatRecodTableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataAry.count+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChatRecodeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ChatRecodeTableViewCell"];
    if (!cell) {
        cell = [[ChatRecodeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ChatRecodeTableViewCell"];
    }

   
    if (indexPath.row == 0) {
         BTMessageModel * msgModel = self.dataAry[indexPath.row];
        cell.titleLbl.hidden = NO;
        cell.nickNameLbl.hidden = YES;
        cell.contentLbl.hidden = YES;
        cell.timeLbl.hidden = YES;
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (msgModel.chatType == ChatTypeFriend) {
            if ([msgModel.head containsString:@"http://"] ||[msgModel.head containsString:@"https://"]) {
                [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",msgModel.head]] placeholderImage:[UIImage imageNamed:@"bg_pyqtx"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                    
                    cell.headImageView.image = image;
                }];
            }else{
                [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",userHeaderImage,msgModel.head]] placeholderImage:[UIImage imageNamed:@"bg_pyqtx"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                    
                    cell.headImageView.image = image;
                }];
              
            }
           
            
            _titleStr = msgModel.nickname;
            cell.titleLbl.text = [NSString stringWithFormat:@"“%@”的记录",_titleStr];
           
        }else{
            ContactModel * groupModel = [[UserDataBaseTool shareUserDataBaseTool] queryGroup:msgModel.gId];
                     NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",userHeaderImage,groupModel.groupHead]]];
                  cell.headImageView.image = [UIImage imageWithData:data];
            _titleStr = groupModel.groupName;
            cell.titleLbl.text = [NSString stringWithFormat:@"“%@”的记录",groupModel.groupName];
        }
      
    }else{
        cell.titleLbl.hidden = YES;
    BTMessageModel * msgModel = self.dataAry[indexPath.row-1];
     cell.model = msgModel;
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 66.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ChatViewController * chatVC = [[ChatViewController alloc]init];
    if (indexPath.row >0) {

    BTMessageModel * model = self.dataAry[indexPath.row-1];
    if (model.chatType == ChatTypeFriend) {
        chatVC.receiverId = model.rfuuid;
        chatVC.chatType = ChatTypeFriend;
        chatVC.destinationTimestamp = model.timestamp;
        
    }else{
        chatVC.receiverId = model.gId;
        chatVC.chatType = ChatTypeGroup;
        chatVC.destinationTimestamp = model.timestamp;
    }
        chatVC.gk_navTitle = _titleStr;
    [self.navigationController pushViewController:chatVC animated:YES];
        }
}

- (UITableView *)chatRecodTableView{
    if (!_chatRecodTableView) {
        _chatRecodTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.gk_navigationBar.bottom, kScreenWidth, kScreenHeight-kTabBarArcHeight) style:UITableViewStyleGrouped];
        _chatRecodTableView.delegate = self;
        _chatRecodTableView.dataSource = self;
        _chatRecodTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _chatRecodTableView.tableFooterView = [UIView new];
    }
    return _chatRecodTableView;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
